package com.example.administrator.chesstest01;

import android.graphics.Point;
import android.widget.EditText;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/7/6.
 */
public class AnalysisJson {

//    创建房间发送和返回
//    {"cmd":0,"name":"123"}
//    {"room_info":{"room_id":100,"room_name":"123"},"cmd":100,"status":0}
    public static void CreateRoom(EditText etRoomName, MyService.OptBinder binder){
        JSONObject object = new JSONObject();
        try {
            object.put("cmd",Common.JsonCmdReq.CREATE_ROOM);
            object.put("name",etRoomName.getText().toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        binder.sendData(object.toString());
    }
    public static void AnalysisRoomList(ArrayList<RoomMessageItem> arrayList , String jsonStr){
        try {
            JSONObject jo = new JSONObject(jsonStr);
            JSONArray ja = jo.getJSONArray("room_list");
            for (int i = 0 ; i <ja.length();i++){
                JSONObject jsonObject = ja.getJSONObject(i);
                int roomId = jsonObject.getInt("room_id");
                String roomName = jsonObject.getString("room_name");
                int userNumber = jsonObject.getInt("player_number");
                RoomMessageItem item = new RoomMessageItem(roomId,roomName,userNumber);
                arrayList.add(item);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
//    请求房间列表和返回
//    {"cmd":1,"name":"123"}
//    {"cmd":101,"room_list":[{"room_id":0,"room_name":"wutao","player_number":2}]}
    public static void ApplyForRoomList(MyService.OptBinder binder){
        JSONObject joGetList = new JSONObject();
        try {
            joGetList.put("cmd",Common.JsonCmdReq.APPLY_FOR_ROOM_LIST);
            joGetList.put("name","123");
            binder.sendData(joGetList.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
//    加入房间请求和返回
//    {"room_id":9,"cmd":2,"name":"123"}
//    {"room_info":{"room_id":9,"room_name":"11111111111"},"cmd":102,"status":0}
    public static void JoinRoom(ArrayList<RoomMessageItem> arrayList, MyService.OptBinder binder, int position) {
        JSONObject joinRoom = new JSONObject();
        try {
            joinRoom.put("cmd",2);
            joinRoom.put("name","123");
            joinRoom.put("room_id",arrayList.get(position).getRoomId());
            binder.sendData(joinRoom.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        System.out.println("--------"+arrayList.get(position).getRoomId());
    }
//获取房间号ID
    public static int ExitRoomId (String jsonStr,int room_id){
        try {
            JSONObject jo = new JSONObject(jsonStr);
            System.out.println("----------"+jo.get("room_info"));
            JSONObject object = new JSONObject(jo.get("room_info").toString());
            room_id = object.getInt("room_id");
            System.out.println("---------ob="+object.getString("room_id"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return room_id;
    }
//    发送退出房间请求
//    {"room_id":9,"cmd":3,"name":"123"}
    public static void SendExitRoom(String name, int id, MyService.OptBinder binder){
        JSONObject object = new JSONObject();
        try {
            object.put("cmd",Common.JsonCmdReq.APPLY_FOR_EXIT_ROOM);
            object.put("name",name);
            object.put("room_id",id);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        binder.sendData(object.toString());
    }
//    发送准备命令
//    {"is_ready":true,"cmd":4,"name":"123"}
    public static void Ready(String name, MyService.OptBinder binder){
        JSONObject object = new JSONObject();
        try {
            object.put("cmd",Common.JsonCmdReq.READY);
            object.put("name",name);
            object.put("is_ready",true);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        binder.sendData(object.toString());
    }
//    对手状态
    public static boolean isOpponentReady(String data){
        boolean isOpponentReady = false;
        try {
            JSONObject jo = new JSONObject(data);
            isOpponentReady = jo.getBoolean("is_opponent_ready");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return isOpponentReady;
    }
//    发送和接收棋子信息
//    {"cmd":4, "name":"孙悟空", "white_chess_list":[{"x":1,"y":1},{"x":1,"y":2}],"black_chess_list":[{"x":0,"y":1},{"x":0,"y":2}]}
    public static void SendChessMessage(String name, MyService.OptBinder binder,ArrayList<Point> jsonArrayWhite,ArrayList<Point> jsonArrayBlack){
        JSONObject object = new JSONObject();
        try {
            object.put("cmd",Common.JsonCmdReq.SEND_CHESS);
            object.put("name",name);
            JSONArray joWhite = new JSONArray();
            JSONArray joBlack = new JSONArray();
            for (int i = 0 ; i < jsonArrayWhite.size();i++){
                JSONObject jsonObjectWhite = new JSONObject();
                jsonObjectWhite.put("x",jsonArrayWhite.get(i).x);
                jsonObjectWhite.put("y",jsonArrayWhite.get(i).y);
                joWhite.put(jsonObjectWhite);
            }

            for (int i = 0 ; i < jsonArrayBlack.size();i++){
                JSONObject jsonObjectBlack = new JSONObject();
                jsonObjectBlack.put("x",jsonArrayBlack.get(i).x);
                jsonObjectBlack.put("y",jsonArrayBlack.get(i).y);
                joBlack.put(jsonObjectBlack);
            }
            object.put("white_chess_list",joWhite);
            object.put("black_chess_list",joBlack);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        binder.sendData(object.toString());
    }
    public static void ReceiveChessMessage(String data,ArrayList<Point> jsonArrayWhite,ArrayList<Point> jsonArrayBlack){
        try {
            JSONObject object = new JSONObject(data);
            JSONArray joWhite = object.getJSONArray("white_chess_list");
            for (int i = 0 ; i <joWhite.length();i++){
                JSONObject jsonObjectWhite = joWhite.getJSONObject(i);
                jsonObjectWhite.getInt("x");
                jsonObjectWhite.getInt("y");
                Point point = new Point(jsonObjectWhite.getInt("x"),jsonObjectWhite.getInt("y"));
                jsonArrayWhite.add(point);
            }
            JSONArray joBlack = object.getJSONArray("black_chess_list");
            for (int i = 0 ; i <joBlack.length();i++){
                JSONObject jsonObjectBlack = joBlack.getJSONObject(i);
                jsonObjectBlack.getInt("x");
                jsonObjectBlack.getInt("y");
                Point point = new Point(jsonObjectBlack.getInt("x"),jsonObjectBlack.getInt("y"));
                jsonArrayBlack.add(point);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

}
